'use strict';
const Controller = require('egg').Controller;

class NoteController extends Controller {
  async index() {
    const { ctx, app, config } = this;
    const { dir, name } = ctx.params;
    const sourcePath = `${config.mdPath}/${dir}/${name}.md`;

    const body = await app.middleware.mloader(sourcePath)();

    if (!body) {
      ctx.result({ state: 404, msg: '没有找到资源' });
    } else {
      ctx.result({ body });
    }

    const { url, ip, originalUrl } = ctx.request;
    ctx.helper.printLogger({ url, ip, originalUrl });
  }

  async catalogue() {
    const { ctx, config, app } = this;
    const sourcePath = `${config.mdPath}/note`;
    const body = await app.middleware.readMdCatalogue(sourcePath)();
    ctx.result({ body });
  }
}

module.exports = NoteController;
